<?php

/**
 * Build a views stack
 * @param String $prefix Unique key prefix
 * @param int $last_modified Max modified date of pushed files
 * @author Pablo
 * @since 1.0
 */
class blgViewsStack extends blgStack{
	protected $prefix;
	protected $last_modified;
	
	/**
	 * Construct a stack for views
	 * @param String $prefix
	 * @author Pablo
	 * @since 1.0
	 */
	function __construct($prefix){
		parent::__construct();
		$this->prefix= $prefix;
		$this->last_modified= 0;				
	}
	
	/**
	 * Add a view to the stack
	 * @param $view_name
	 * @see engine/clases/blgStack::push()
	 * @author Pablo
	 * @since 1.0
	 */
	public function push($view_name){
		global $_VIEWS;
		
		parent::push($view_name, FALSE);
		$file= $_VIEWS->path($view_name);
		$time_modified= filemtime($file);
		
		if(!empty($time_modified) && $time_modified>$this->last_modified)
			$this->last_modified= $time_modified;
	}	
	
	/**
	 * Get max modfied time in the stack
	 * @return int Unix time
	 * @author Pablo
	 * @since 1.0
	 */
	public function last_modified(){
		return $this->last_modified;		
	}
	
	/**
	 * Return a UNIQUE key for a stack depending on the elements pushed in it and the max modified date
	 * return String
	 * @author Pablo
	 * @since 1.0
	 */
	public function key(){
		$files= $this->stack; 
		sort($files, SORT_STRING);
		return $this->prefix.'_'.md5(serialize($files).$this->last_modified);		
	}
}